<?php
//TODO repenser tout le systéme pour faire marcher tous les inputs exotiques (select, checkbox, combobox...) [galère :/]
//TODO faire un bon gros refactor avec des commentaires !
class FormulaireHelper{
	
	private $listeElements = null;
	
	private $pageConfig = null;
	private $idForm = null;
	private $isPost = null;
	private $method = null;
	private $action = null;
	private $enctype = null;
	private $listNotifications = null;
	
	public function __construct($id, $method, $action, $enctype){

		session_start();

		$this->idForm = $id;
		
		if( isset( $_SESSION[$this->getSeedIdElement('sessionListNotifications')] ) ){
			$this->listNotifications = $_SESSION[$this->getSeedIdElement('sessionListNotifications')];
		}
		
		$this->method = $method;
		
		if($this->method === 'POST'){
			$this->isPost = true;
		}else{
			$this->isPost = false;
		}
		
		$this->action = $action;
		$this->enctype = $enctype;
		
		$this->listeElements = array();
		
	}
	
//TODO faire de meme avec les requêtes ajax
//TODO mettre un div autour des messages générés, avec une classe histoire de pouvoir mettre les messages en js en réupérant l'élément dans le dom plutôt q'un alert dégeu...
//TODO rajouter un truc qui emp�che de valider le formulaire si c'est pas valable en js (chaud :/)
	public function generateJS(){
		?>
	<script>
		$(document).ready(function(){
		<?php 
			
			foreach($this->listeElements as $element){
				$element->generateJS();
			}
		
		?>
		});
	</script>
		<?php
	}
	
	public function checkAjax(){
		
		$idElement = $this->getValue('idElementAjax');
		
		if(isset($idElement)){
			$valueElement = $this->getValue('value');
			
			$element = $this->listeElements[$idElement];
			$result = $element->check($valueElement);
			
			$this->listNotifications[$idElement] = $result;
			$_SESSION[$this->getSeedIdElement('sessionListNotifications')] = $this->listNotifications;
			
			header('Content-Type: application/json');
			echo json_encode($result);
			
		}

	}
	
	public function check(){
		
		$listNotifications = array();
		
		foreach($this->listeElements as $element){
			
			$result = null;
			$idElementTmp = $element->getId();
			
			$result = $element->check($this->getValue($idElementTmp));
			
			//ajout de la liste des messages de l'élément dans la liste.
			if(!empty($result)){
				$listNotifications[$idElementTmp] = $result;
			}
		}
		
		$this->listNotifications = $listNotifications;
		$_SESSION[$this->getSeedIdElement('sessionListNotifications')] = $this->listNotifications;
		
		return $this->listNotifications;
	}
	
	public function getStrongness($idElement, $nameInstanceValidator){
		
		$idElement = $this->getSeedIdElement($idElement);
		
		if(!isset($this->listNotifications)){	return null;	}
		
		foreach($this->listNotifications[$idElement] as $notification){
			
			if($notification['validatorInstance'] == $nameInstanceValidator){
				
				return $notification['strongness'];
			}
		}
		
		return null;
	}
		
	public function hasError(){
		
		$hasErrors = false;
		
		foreach($this->listeElements as $element){
			
			$idElementTmp = $element->getId();
			
			if(isset ($this->listNotifications[$idElementTmp])){
				
				//parcours de la liste de notifications de l'élément (possible d'en avoir un par validator)
				foreach($this->listNotifications[$idElementTmp] as $notification){
					if($notification['isRefuse']){
						$hasErrors = true;
					}
				}
			}

		}
		
		return $hasErrors;
	}
	
	private function getSeedIdElement($idElement){
		
		return $this->idForm . '_' . $idElement;
	}
	
	public function addElement($element){
		
		$newidElement = $this->getSeedIdElement($element->getId());
		
		$element->setId( $newidElement );
				
		$this->listeElements[$newidElement] = $element;
	}
	
	public function generate($idElement){
		
		$this->listeElements[$this->getSeedIdElement($idElement)]->generate();
	}
	
	public function generateLabel($idElement){
		
		$element = $this->listeElements[$this->getSeedIdElement($idElement)];
		
		if($element->getLabel() != null){
			echo '<label class="labelSimpleHelper" for="'.$element->getId().'">'.$element->getLabel().'</label>';
		}
	}
	
	public function generateValidationMessage($idElement){
		
		$idElement = $this->getSeedIdElement($idElement);
		
		if(isset($this->listNotifications[$idElement])){
			
			$listNotificationsElement = $this->listNotifications[$idElement];
	
			if(isset($listNotificationsElement)){
				
				foreach($listNotificationsElement as $notificationElement){
					
					echo $notificationElement['message'], '<br />';
					
				}
				
			}
			
		}
		
	}
	
	public function generateDebutForm(){
		
		echo '<form id="'.$this->idForm.'" name="'.$this->idForm.'" method="'.$this->method.'" action="'.$this->action.'" enctype="'.$this->enctype.'">';
	}
	
	public function generateFinForm(){
		
		echo '<input type="hidden"  name="formCibleInitiale"  value="'.$_SERVER['PHP_SELF'].'">';
		echo '</form>';
	}
	
	
	
	public function getValue($idElement){
	
		if( ($this->isPost) && (isset($_POST[$idElement])) ){
				
			return $_POST[$idElement];
				
		}else if( isset($_GET[$idElement]) ){
				
			return $_GET[$idElement];
				
		}else{
				
			return null;
				
		}
	
	}
	
	public function dataInvalid(){
		
		header('Location: ' . $this->getValue(formCibleInitiale));
	}

	
}